<template>
  <div class="chart-container">
    <div class="scroll-wrapper">
      <div class="chart-header">
        <div class="celestial-decoration">
          <span class="decoration-symbol">✧</span>
          <span class="decoration-line">✦✦✦</span>
          <span class="decoration-symbol">☯</span>
          <span class="decoration-line">✦✦✦</span>
          <span class="decoration-symbol">✧</span>
        </div>
        <h2 class="chart-title">紫微命盘解析</h2>
        <div class="celestial-decoration">
          <span class="decoration-line">✦✦✦</span>
          <span class="decoration-symbol">✦</span>
          <span class="decoration-line">✦✦✦</span>
        </div>
      </div>
      
      <div class="user-info-card">
        <div class="user-info-content">
          {{ formattedUserInfo }}
        </div>
      </div>
      
      <div class="tabs-container">
        <nav class="tab-navigation">
          <button 
            v-for="tab in tabs" 
            :key="tab.id"
            @click="activeTab = tab.id"
            :class="[
              'tab-button',
              activeTab === tab.id ? 'tab-active' : 'tab-inactive'
            ]"
          >
            <span class="tab-text">{{ tab.name }}</span>
          </button>
        </nav>
      </div>
      
      <!-- 命盘内容区域 -->
      <div class="chart-content">
        <!-- 宫位排盘 -->
        <div v-if="activeTab === 'palace'" class="animate-fade-in">
          <PalaceGrid :chartData="chartData" />
        </div>
        
        <!-- 图形命盘 -->
        <div v-if="activeTab === 'circle'" class="animate-fade-in">
          <CircleChart :chartData="chartData" :userInfo="userInfo" />
        </div>
        
        <!-- 大限流年 -->
        <div v-if="activeTab === 'timeline'" class="animate-fade-in">
          <TimeLine :chartData="chartData" :userInfo="userInfo" />
        </div>
        
        <!-- 命盘解析 -->
        <div v-if="activeTab === 'reading'" class="animate-fade-in">
          <ReadingAnalysis :chartData="chartData" :userInfo="userInfo" />
        </div>

      </div>
      
      <!-- 操作按钮 -->
      <div class="action-buttons">
        <button @click="saveChart" class="mystical-button">
          <i class="fas fa-save button-icon"></i> 保存命盘
        </button>
        
        <button @click="shareChart" class="mystical-button">
          <i class="fas fa-share-alt button-icon"></i> 分享命盘
        </button>
        
        <button @click="showDetailModal" class="mystical-button">
          <i class="fas fa-book-open button-icon"></i> 详细解读
        </button>
      </div>
      
      <p class="disclaimer-text">
        注：虽然由研究20年的紫微斗数申大师开发，本命盘仅供娱乐参考，生活工作请理性决策。
      </p>
      
      <!-- 底部装饰 -->
      <div class="scroll-footer">
        <div class="celestial-decoration">
          <span class="decoration-line">✦✦✦✦✦</span>
        </div>
      </div>
    </div>
      
    <!-- 详细解读弹窗 -->
    <div v-if="showModal" class="modal-backdrop" @click.self="showModal = false">
      <div class="modal-scroll" @click.stop>
        <div class="modal-header">
          <h3 class="modal-title">紫微命盘详细解读</h3>
          <button @click="showModal = false" class="close-button">
            <i class="fas fa-times"></i>
          </button>
        </div>
        
        <div class="modal-content">
          <div class="reading-section">
            <h4 class="section-title">
              <span class="section-icon">☯</span> 命宫解读
            </h4>
            <div class="section-content" v-html="detailContent.mingGong"></div>
          </div>
          
          <div class="reading-section">
            <h4 class="section-title">
              <span class="section-icon">💰</span> 财富运势
            </h4>
            <div class="section-content" v-html="detailContent.wealth"></div>
          </div>
          
          <div class="reading-section">
            <h4 class="section-title">
              <span class="section-icon">🏆</span> 事业发展
            </h4>
            <div class="section-content" v-html="detailContent.career"></div>
          </div>
          
          <div class="reading-section">
            <h4 class="section-title">
              <span class="section-icon">❤️</span> 婚姻情感
            </h4>
            <div class="section-content" v-html="detailContent.relationship"></div>
          </div>
          
          <div class="reading-section">
            <h4 class="section-title">
              <span class="section-icon">🌿</span> 健康提示
            </h4>
            <div class="section-content" v-html="detailContent.health"></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import PalaceGrid from './PalaceGrid.vue';
import CircleChart from './CircleChart.vue';
import TimeLine from './TimeLine.vue';
import ReadingAnalysis from './ReadingAnalysis.vue';
import BaziChart from './BaziChart.vue';
import html2canvas from 'html2canvas';

export default {
  name: 'ChartDisplay',
  components: {
    PalaceGrid,
    CircleChart,
    TimeLine,
    ReadingAnalysis,
    BaziChart
  },
  props: {
    chartData: Object,
    userInfo: Object
  },
  data() {
    return {
      activeTab: 'palace',
      showModal: false,
      tabs: [
        { id: 'palace', name: '宫位排盘' },
        { id: 'circle', name: '图形命盘' },
        { id: 'timeline', name: '大限流年' },
        { id: 'reading', name: '命盘解析' },
        // { id: 'bazi', name: '八字排盘' }
      ],
      detailContent: {
        mingGong: '',
        wealth: '',
        career: '',
        relationship: '',
        health: ''
      }
    };
  },
  computed: {
    formattedUserInfo() {
      if (!this.userInfo) return '';
      
      const genderText = this.userInfo.gender === 'male' ? '男' : '女';
      const calendarType = this.userInfo.birthSystem === 'solar' ? '阳历' : '农历';
      
      return `${this.userInfo.name} (${genderText}) - ${calendarType}: ${this.userInfo.birthYear}年${this.userInfo.birthMonth}月${this.userInfo.birthDay}日 - 出生地: ${this.userInfo.birthPlace || '未填写'}`;
    }
  },
  methods: {
    saveChart() {
      const chartElement = document.querySelector('.chart-container');
      if (!chartElement) return;
      
      html2canvas(chartElement).then(canvas => {
        const link = document.createElement('a');
        link.download = `${this.userInfo.name}-紫微命盘.png`;
        link.href = canvas.toDataURL('image/png');
        link.click();
      });
    },
    
    shareChart() {
      // 生成分享链接
      const shareData = btoa(JSON.stringify({
        userInfo: this.userInfo,
        timestamp: Date.now()
      }));
      
      const shareUrl = `${window.location.origin}${window.location.pathname}?share=${shareData}`;
      
      // 复制到剪贴板
      navigator.clipboard.writeText(shareUrl).then(() => {
        alert('分享链接已复制到剪贴板');
      }).catch(() => {
        alert('分享链接：' + shareUrl);
      });
    },
    
    showDetailModal() {
      // 生成详细解读内容
      this.generateDetailContent();
      this.showModal = true;
    },
    
    generateDetailContent() {
      const data = this.chartData;
      if (!data) return;
      
      // 这里应该根据命盘数据生成详细解读内容
      // 以下为示例内容
      this.detailContent = {
        mingGong: `<p><strong>${this.userInfo.name}</strong>的命宫位于${this.getMingGongDescription(data)}</p>
                   <p>紫微星系与天府星系在命宫的相互组合，形成了您性格中的核心特质。您为人处事谨慎有度，心思细腻，有较强的洞察力。</p>`,
        
        wealth: `<p>您的财帛宫位于${this.getWealthDescription(data)}</p>
                <p>财运方面，您需要注意积累与规划，不宜投机取巧。</p>`,
        
        career: `<p>您的官禄宫显示您在事业上有较强的组织能力和领导潜力。</p>
                <p>职业发展上，适合从事需要细致分析和长期积累的行业。</p>`,
        
        relationship: `<p>您的夫妻宫显示在情感关系中，您注重精神层面的交流与共鸣。</p>
                      <p>在婚姻中，您会是一个忠诚且负责任的伴侣。</p>`,
        
        health: `<p>您的命盘显示需要特别注意消化系统和呼吸系统的健康。</p>
                <p>建议保持规律作息，适当参加有氧运动，增强体质。</p>`
      };
    },
    
    getMingGongDescription(data) {
      const mingGong = data.palaces.find(p => p.name === '命宫');
      if (!mingGong) return '';
      
      const earthlyBranches = ['子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥'];
      const branch = earthlyBranches[(mingGong.position - 1) % 12];
      
      // 获取命宫主星
      const mainStars = [];
      for (const [star, pos] of Object.entries(data.stars)) {
        if (pos === mingGong.position && ['紫微', '天机', '太阳', '武曲', '天同', '廉贞', '天府', '太阴', '贪狼', '巨门', '天相', '天梁', '七杀', '破军'].includes(star)) {
          mainStars.push(star);
        }
      }
      
      const starDesc = mainStars.length > 0 ? `，主星有${mainStars.join('、')}` : '';
      
      return `${branch}宫${starDesc}`;
    },
    
    getWealthDescription(data) {
      const wealthPalace = data.palaces.find(p => p.name === '财帛宫');
      if (!wealthPalace) return '';
      
      const earthlyBranches = ['子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥'];
      return earthlyBranches[(wealthPalace.position - 1) % 12] + '宫';
    }
  }
};
</script>

<style scoped>
.chart-container {
  position: relative;
  font-family: 'SimSun', 'STFangsong', serif;
  color: #3e2723;
}

.scroll-wrapper {
  background-image: url('');
  background-color: #fcf6eb;
  border: 2px solid #d6b78e;
  border-radius: 8px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1), 0 0 30px rgba(210, 175, 125, 0.2);
  padding: 2rem;
  position: relative;
  margin: 0 auto;
}

.scroll-wrapper::before, .scroll-wrapper::after {
  content: '';
  position: absolute;
  width: 20px;
  height: 100%;
  top: 0;
  background-image: url('');
  z-index: 1;
}

.scroll-wrapper::before {
  left: 0;
  border-right: 1px solid #d6b78e;
}

.scroll-wrapper::after {
  right: 0;
  border-left: 1px solid #d6b78e;
}

.chart-header {
  text-align: center;
  margin-bottom: 2rem;
  position: relative;
}

.chart-title {
  font-size: 1.8rem;
  font-weight: bold;
  color: #8b3e2f;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
  margin: 0.5rem 0;
  letter-spacing: 4px;
}

.celestial-decoration {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0.8rem 0;
}

.decoration-symbol {
  font-size: 1.2rem;
  color: #a46628;
}

.decoration-line {
  margin: 0 0.8rem;
  color: #a46628;
  letter-spacing: 4px;
}

.user-info-card {
  background: linear-gradient(135deg, rgba(146, 72, 36, 0.1) 0%, rgba(164, 102, 40, 0.2) 100%);
  border: 1px solid #d6b78e;
  border-radius: 6px;
  padding: 1rem;
  margin-bottom: 2rem;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.user-info-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url('');
  opacity: 0.3;
}

.user-info-content {
  position: relative;
  z-index: 1;
  font-size: 1rem;
  font-weight: 500;
  color: #66331E;
}

.tabs-container {
  margin-bottom: 2rem;
  border-bottom: 1px solid #d6b78e;
}

.tab-navigation {
  display: flex;
  flex-wrap: wrap;
  margin-bottom: -1px;
}

.tab-button {
  padding: 0.7rem 1.5rem;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  font-size: 1rem;
  font-weight: 500;
  color: #8b5a2b;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  font-family: 'SimSun', 'STFangsong', serif;
}

.tab-button::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 0;
  height: 2px;
  background-color: #8b3e2f;
  transition: width 0.3s ease;
}

.tab-button:hover::after {
  width: 100%;
}

.tab-active {
  color: #8b3e2f;
  border-bottom-color: #8b3e2f;
  font-weight: bold;
}

.tab-inactive:hover {
  color: #a46628;
}

.chart-content {
  min-height: 400px;
  margin-bottom: 2rem;
}

.action-buttons {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 1rem;
  margin-top: 2rem;
  margin-bottom: 1.5rem;
}

.mystical-button {
  background: linear-gradient(135deg, #8b3e2f 0%, #a46628 100%);
  color: #fff;
  border: none;
  padding: 0.8rem 1.5rem;
  border-radius: 4px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  font-family: 'SimSun', 'STFangsong', serif;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  position: relative;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(164, 102, 40, 0.3);
}

.mystical-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: 0.5s;
}

.mystical-button:hover::before {
  left: 100%;
}

.mystical-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(164, 102, 40, 0.4);
}

.button-icon {
  margin-right: 0.5rem;
}

.disclaimer-text {
  text-align: center;
  font-size: 0.9rem;
  color: #8b5a2b;
  font-style: italic;
  margin-top: 1.5rem;
}

.scroll-footer {
  text-align: center;
  margin-top: 2rem;
}

/* Modal styles */
.modal-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: fadeIn 0.3s ease-in-out;
}

.modal-scroll {
  background-image: url('');
  background-color: #fcf6eb;
  border: 2px solid #d6b78e;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2), 0 0 40px rgba(139, 62, 47, 0.1);
  max-width: 700px;
  width: 90%;
  max-height: 90vh;
  overflow-y: auto;
  position: relative;
  animation: zoomIn 0.3s ease-in-out;
}

.modal-header {
  padding: 1.2rem 1.5rem;
  border-bottom: 1px solid #d6b78e;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: sticky;
  top: 0;
  background-color: #fcf6eb;
  z-index: 10;
}

.modal-title {
  font-size: 1.4rem;
  font-weight: bold;
  color: #8b3e2f;
  margin: 0;
}

.close-button {
  background: none;
  border: none;
  color: #8b5a2b;
  font-size: 1.2rem;
  cursor: pointer;
  transition: color 0.3s ease;
}

.close-button:hover {
  color: #8b3e2f;
}

.modal-content {
  padding: 1.5rem;
}

.reading-section {
  margin-bottom: 1.8rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid #efe0cb;
}

.reading-section:last-child {
  margin-bottom: 0;
  padding-bottom: 0;
  border-bottom: none;
}

.section-title {
  font-size: 1.2rem;
  font-weight: bold;
  color: #8b3e2f;
  margin-bottom: 1rem;
  display: flex;
  align-items: center;
}

.section-icon {
  margin-right: 0.6rem;
  font-size: 1.1rem;
}

.section-content {
  color: #5d4037;
  line-height: 1.6;
}

/* Animations */
.animate-fade-in {
  animation: fadeIn 0.5s ease-in-out;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes zoomIn {
  from {
    opacity: 0;
    transform: scale(0.95);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

/* Responsive adjustments */
@media (max-width: 768px) {
  .scroll-wrapper {
    padding: 1.5rem;
  }
  
  .tab-button {
    padding: 0.6rem 1rem;
    font-size: 0.9rem;
  }
  
  .action-buttons {
    flex-direction: column;
    align-items: center;
  }
  
  .mystical-button {
    width: 100%;
    justify-content: center;
  }
}
</style>